﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace DzInfo.TuiLing
{
    public class K3CloudHttpClient
    {
        public string Url { get; set; }
        public string Content { get; set; }
        static CookieContainer Cookie = new CookieContainer();

        public string SysncRequest()
        {
            try
            {
                HttpWebRequest httpRequest = WebRequest.Create(Url) as HttpWebRequest;
                httpRequest.Method = "POST";
                httpRequest.ContentType = "application/json; charset=utf-8";
                httpRequest.CookieContainer = Cookie;
                httpRequest.Timeout = 1000 * 60 * 10;

                byte[] bytes = Encoding.UTF8.GetBytes(Content);
                httpRequest.ContentLength = bytes.Length;

                using (Stream reqStream = httpRequest.GetRequestStream())
                {
                    reqStream.Write(bytes, 0, bytes.Length);
                    reqStream.Flush();
                }

                using (WebResponse response = httpRequest.GetResponse())
                using (Stream repStream = response.GetResponseStream())
                using (var reader = new StreamReader(repStream, Encoding.UTF8))
                {
                    string result = reader.ReadToEnd();
                    return result;
                }
            }
            catch (Exception ex)
            {
                return $"请求异常: {ex.Message}";
            }
        }
    }
}
